package com.telnetnz.ticketdesk.service;

import java.util.ArrayList;
import java.util.List;

import net.sf.ehcache.Element;

import org.apache.log4j.Logger;

import com.telnetnz.ticketdesk.bean.Tag;
import com.telnetnz.ticketdesk.bean.TicketToTag;
import com.telnetnz.ticketdesk.common.config.SpringContext;
import com.telnetnz.ticketdesk.dao.entity.TicketToTagEntity;
import com.telnetnz.ticketdesk.dao.wrapper.TicketToTagWrap;

public class TicketToTagService
{
	private static Logger logger = Logger.getLogger(TicketToTagService.class);
	private static String CACHE_KEY = "id_";
	private static String CACHE_KEY_TK = "tk_";
	private static String CACHE_KEY_TG = "tg_";

	/**
	 * Important: This is the only entry to get the project bean, including the methods that return instances list
	 * 
	 * @param uid
	 * @return
	 */
	public static TicketToTag getTicketToTag(long ticketId, long tagId)
	{
		Element elm = getWrap().getCache().get(CACHE_KEY + ticketId + "_" + tagId);
		TicketToTag bean = null;
		if (elm == null)
		{
			TicketToTagEntity entity = getWrap().selectByTicketIdAndTagId(ticketId, tagId);
			if (entity != null)
			{
				bean = TicketToTag.adapt(entity);
				getWrap().getCache().put(new Element(CACHE_KEY + ticketId + "_" + tagId, bean));
			}
		}
		else
		{
			bean = (TicketToTag) elm.getValue();
		}
		return bean;
	}

	public static void add(long ticketId, long tagId, String tatName)
	{
		getWrap().insert(ticketId, tagId, tatName);
		getWrap().getCache().remove(CACHE_KEY + ticketId + "_" + tagId);
		getWrap().getCache().remove(CACHE_KEY_TK + ticketId);
		getWrap().getCache().remove(CACHE_KEY_TG + tagId);
	}

	public static void delete(long ticketId, long tagId)
	{
		getWrap().deleteTicketToTag(ticketId, tagId);
		getWrap().getCache().remove(CACHE_KEY + ticketId + "_" + tagId);
		getWrap().getCache().remove(CACHE_KEY_TK + ticketId);
		getWrap().getCache().remove(CACHE_KEY_TG + tagId);
	}

	public static List<Tag> getTagsByTicketId(long ticketId)
	{
		List<TicketToTag> ttts = getByTicketId(ticketId);
		List<Tag> tags = new ArrayList<Tag>();
		for (TicketToTag ttt : ttts)
			tags.add(ttt.getTag());
		return tags;
	}

	public static List<TicketToTag> getByTicketId(long ticketId)
	{
		Element elm = getWrap().getCache().get(CACHE_KEY_TK + ticketId);
		List<TicketToTag> bean;
		if (elm == null)
		{
			List<TicketToTagEntity> entities = getWrap().selectByTicketId(ticketId);
			bean = adaptList(entities);
			getWrap().getCache().put(new Element(CACHE_KEY_TK + ticketId, bean));
		}
		else
		{
			bean = (List<TicketToTag>) elm.getValue();
		}
		return bean;
	}

	public static void deleteByTicketId(long ticketId)
	{
		// Delete the relevant cache of this associations before the main deletion, otherwise the relevance can not be retrieved.
		List<TicketToTag> ttts = getByTicketId(ticketId);
		for (TicketToTag ttt : ttts)
			getWrap().getCache().remove(CACHE_KEY_TG + ttt.getTagId());

		// Now delete the association and its cache
		getWrap().deleteByTicketId(ticketId);
		getWrap().getCache().remove(CACHE_KEY_TK + ticketId);
	}

	public static List<TicketToTag> getByTagId(long tagId)
	{
		Element elm = getWrap().getCache().get(CACHE_KEY_TG + tagId);
		List<TicketToTag> bean;
		if (elm == null)
		{
			List<TicketToTagEntity> entities = getWrap().selectByTagId(tagId);
			bean = adaptList(entities);
			getWrap().getCache().put(new Element(CACHE_KEY_TG + tagId, bean));
		}
		else
		{
			bean = (List<TicketToTag>) elm.getValue();
		}
		return bean;
	}

	public static void deleteByTagId(long tagId)
	{
		// Delete the relevant cache of this associations before the main deletion, otherwise the relevance can not be retrieved.
		List<TicketToTag> ttts = getByTagId(tagId);
		for (TicketToTag ttt : ttts)
			getWrap().getCache().remove(CACHE_KEY_TK + ttt.getTicketId());

		getWrap().deleteByTagId(tagId);
		getWrap().getCache().remove(CACHE_KEY_TG + tagId);
	}

	private static List<TicketToTag> adaptList(List<TicketToTagEntity> entities)
	{
		List<TicketToTag> beans = new ArrayList<TicketToTag>();
		if (entities != null && entities.size() > 0)
		{
			for (TicketToTagEntity entity : entities)
			{
				TicketToTag bean = getTicketToTag(entity.getTicketId(), entity.getTagId());
				if (bean != null)
					beans.add(bean);
			}
		}
		return beans;
	}

	private static TicketToTagWrap getWrap()
	{
		return SpringContext.getTicketToTagWrap();
	}

	public static void main(String[] args)
	{
		
	}
}
